using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using Publicuse.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Util
{
    public class JwtUtil
    {
        /// <summary>
        /// 根据jwtToken 获取实体
        /// </summary>
        /// <param name="token">jwtToken</param>
        /// <returns></returns>
        public static JwtToken GetJwtDecode(string token)
        {
            var secret = AppConfigUtil.ReadAppSettings("JWTTokenOptions", "SecurityKey");
            IJsonSerializer serializer = new JsonNetSerializer();
            IDateTimeProvider provider = new UtcDateTimeProvider();
            IJwtValidator validator = new JwtValidator(serializer, provider);
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
            IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);
            var data = decoder.DecodeToObject<JwtToken>(token, secret, verify: true);//token为之前生成的字符串
            return data;
        }
    }
}
